/*******************************************************************************
 * Copyright (c) 2004, 2005 Jean-Michel Lemieux, Jeff McAffer and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Hyperbola is an RCP application developed for the book
 *     Eclipse Rich Client Platform -
 *         Designing, Coding, and Packaging Java Applications
 *
 * Contributors:
 *     Jean-Michel Lemieux and Jeff McAffer - initial implementation
 *******************************************************************************/
package com.balance.anole.erp.common.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.balance.anole.erp.common.model.ProductItemVO;
import com.balance.anole.erp.util.Messages;
import com.balance.anole.erp.util.ServicesCenter;


/**
 * Login dialog, which prompts for the user's account info, and has Login and
 * Cancel buttons.
 */
public class ProductSelector2 extends Dialog {

	private boolean isClient = false;

	protected int width;

	protected int height;

	private String title;

	private ProductSelectorSearchResult searchResult;

	private ProductSelectorSearchPanel productSelectorSearchPanel;

	public ProductSelector2(int width, int height, Shell parentShell) {
		super(parentShell);
		this.width = width;
		this.height = height;
		this.title = Messages.ProductSelector_Title;
	}

	public ProductSelector2(Shell parentShell) {
		this(600, 600, parentShell);
	}

	/**
	 * @wbp.parser.constructor
	 */
	public ProductSelector2(Shell parentShell, boolean isClient) {
		this(600, 600, parentShell);
		this.isClient = isClient;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(title);
//		newShell.setSize(200, 600);

		// Get the resolution
		Rectangle pDisplayBounds = newShell.getDisplay().getPrimaryMonitor().getBounds();

		int nMinWidth = width;
		int nMinHeight = height;
		// This formulae calculate the shell's Left ant Top
		int nLeft = (pDisplayBounds.width - nMinWidth) / 2;
		int nTop = (pDisplayBounds.height - nMinHeight) / 2;

		// Set shell bounds,
		newShell.setBounds(nLeft, nTop, nMinWidth, nMinHeight);
	}

	protected void createButtonsForButtonBar(Composite parent) {

	}
	protected Control createDialogArea(Composite parent) {

		Composite composite = (Composite) super.createDialogArea(parent);

		composite.setLayout(new FillLayout());

		Composite comp = new Composite(composite, SWT.NONE);
		comp.setLayout(new FormLayout());
		/*final GridData gd_comp = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_comp.heightHint = 272;
		gd_comp.widthHint = 540;
		comp.setLayoutData(gd_comp);*/

		productSelectorSearchPanel = new ProductSelectorSearchPanel(comp, SWT.NONE, isClient);
		FormData fd_productSelectorSearchPanel = new FormData();
		fd_productSelectorSearchPanel.top = new FormAttachment(0, 5);
		fd_productSelectorSearchPanel.left = new FormAttachment(0, 5);
		fd_productSelectorSearchPanel.right = new FormAttachment(100, -5);
		productSelectorSearchPanel.setLayoutData(fd_productSelectorSearchPanel);
		productSelectorSearchPanel.initSearchCriteria();

		searchResult = new ProductSelectorSearchResult(comp, SWT.NONE);
		FormData fd_searchResult = new FormData();
		fd_searchResult.top = new FormAttachment(productSelectorSearchPanel, 10);
		fd_searchResult.left = new FormAttachment(0, 5);
		fd_searchResult.right = new FormAttachment(100, -5);
		fd_searchResult.bottom = new FormAttachment(100, -5);
		searchResult.setLayoutData(fd_searchResult);

		//if user double click on the result item, then value is set, the selector should be closed.
		searchResult.addProductSelectListener(new IChangeListener(){

			@Override
			public void handleChange(ChangeEvent event) {
				close();
			}

		});
		productSelectorSearchPanel.getSearchButton().addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(! productSelectorSearchPanel.isSearchCriteriaValid()){
					MessageDialog.openWarning(null, Messages.ProductSelector_Warn, "请至少输入一个搜索条件！");
					return;
				}

				List<ProductItemVO> productList = null;

				if(isClient){
					com.balance.anole.client.common.service.CommonService cs = ServicesCenter.getServicesCenter().getService(com.balance.anole.client.common.service.CommonService.class);
					productList = cs.getProductItems(productSelectorSearchPanel.getSearchTO());
				}else{
					/*BusinessService as = ServicesCenter.getServicesCenter().getService(BusinessService.class);
					productList = as.getProductItems(productSelectorSearchPanel.getSearchTO());*/
				}

				if(productList == null || productList.size() == 0){
					MessageDialog.openWarning(null, Messages.ProductSelector_Warn, Messages.ProductSelector_Prod_Not_Found_Msg);
					productList = new ArrayList<ProductItemVO>();
				}

				searchResult.getTableViewer().setInput(productList);

				searchResult.getTableViewer().refresh();
			}

		});

		return composite;
	}

	public ProductItemVO getSelectedProduct(){
		return searchResult.getSelectedProduct();
	}
}
